import requests
import os

def upload_file(file_path, url):
   """
   上传文件到指定的URL

   :param file_path: 要上传的文件路径
   :param url: 文件上传的URL
   """
   if not os.path.isfile(file_path):
       print(f"File not found: {file_path}")
       return

   filename = os.path.basename(file_path)
   files = {'file': (filename, open(file_path, 'rb'))}

   try:
       response = requests.post(url, files=files)
       if response.status_code == 200:
           print("Server response:", response.json())
       else:
           print(f"Failed to upload file. Status code: {response.status_code}, Response: {response.text}")
   except Exception as e:
       print(f"An error occurred: {e}")
   finally:
       files['file'][1].close()  # 确保文件被关闭

if __name__ == "__main__":
   upload_file("/root/test2.jpg", "http://47.76.79.104:5008/upload/file")
